<template>
  <div class="my-collect">
    <!-- <div class="new-collect">
      <van-button type="default" block size="small" icon="plus">
        新建收藏夹
      </van-button>
    </div> -->
    <div class="item-avatar-one" v-for="item in list" :key="item.id">
      <div class="top"  @click="goToUser">
        <van-image
          width="0.9rem"
          height="0.9rem"
          fit="cover"
          round
          :src="item.article.user.avatar ? item.article.user.avatar : avatar"
        />
        <div class="author" v-if="item.article.user" >
          <a>{{item.article.user.nickname}}</a>
          <span class="introduce"  @click="onClickFocus(item.article.user_id)">{{isAttentionAuthor ? '已关注  .  ':''}}  {{item.createdAt | relativeTime}}  . {{item.article.user.introduce}}  </span>
        </div>
        <div class="right-bnt">
          <van-button size="small"  @click="onClickFocus(item.article.user_id)" >{{isAttentionAuthor ? '已关注  .  ':'关注'}}</van-button>
        </div>
      </div>
      <div class="centre">
        <div class="left">
          <h1 @click="goToArticle">{{item.article.title}}</h1>
        </div>
        <div class="right" @click="goToArticle">
          <van-image
            width="120"
            height="90"
            fit="cover"
            :src="item.article.cover_img"
          />
        </div>
      </div>
      <div class="bottom">
        <BottomOperation :article='item.article'/>
      </div>
    </div>
    <ShareSheet />
  </div>
</template>

<script>
import BottomOperation from '@/components/User/bottom-operation.vue'
import ShareSheet from '@/views/Article/components/share-sheet.vue'
import {getUserArticleCollections} from '@/api/article'
import { addUserFollowings,deleteUserFollowings,isWage } from '@/api/user'
import {mapGetters} from 'vuex'
export default {
  name: 'my-collect',
  components: {
    BottomOperation,
    ShareSheet
  },
  props:{
    userId:{
      type:Number,
      required:true
    }
  },
  data () {
    return {
      query:{
        pageNum:1, pageSize:10
      },
      list:[],
      total:0,
      avatar:'https://img01.yzcdn.cn/vant/cat.jpeg',
      isAttentionAuthor:false
    }
  },
  computed:{
    ...mapGetters(['token']),
    isgz(id){
      console.log(this.isWage(id));
      return this.isWage(id)
    }
  },
 created () {
   this.getUserArticleCollections(this.userId,this.query)
   this.isWage()
 },
  methods: {
    goToUser () {
      this.$router.push('/user')
    },
    goToArticle () {
      const id = 3
      this.$router.push('/article/' + id)
    },
    async getUserArticleCollections(id,data){
       const res = await getUserArticleCollections(id,data)
       this.query.pageNum = res.pageNum
       this.query.pageSize = res.pageSize
       this.list = res.list
       this.total = res.total
     },
     async isWage(id){
       // if(!this.token) return console.log('去登录');
       const res = await isWage(id)
       if(res){
         this.isAttentionAuthor = true
         return true
          // console.log('当前状态已关注');
       }else{
         // console.log('当前状态未关注');
         this.isAttentionAuthor = false
         return false
       }

     },
     async onClickFocus (author_id) {
       if(!this.token) return console.log('去登录');
       this.isWage(author_id)
       if(this.isAttentionAuthor){
         // 发起请求取消关注
          await deleteUserFollowings(author_id)
         // console.log('取消关注作者成功');
         this.isAttentionAuthor = false
       }else{
         // 发起请求关注
         const re = await addUserFollowings({follerd_id:author_id})
         if(!re) return console.log('关注失败');
         // console.log('关注作者成功');
         this.isAttentionAuthor = true
       }
     },
  }
}
</script>

<style lang="scss" scoped>
.my-collect {
  background-color: rgb(247, 247, 247);
  text-align: left;
}
.new-collect {
  padding-top: 15px;
  margin: 0 15px;
}
.item-avatar-one {
  margin-top: 10px;
  padding: 15px 15px 5px 15px;
  box-sizing: border-box;
  background-color: #fff;
  .top {
    position: relative;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .author {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 10px;
      a {
        font-size: 14px;
        font-weight: 500;
      }
      span {
        font-size: 12px;
        color: #ccc;
      }
      .introduce{
        max-width: 260px;
        overflow: hidden; //溢出内容隐藏
          text-overflow: ellipsis; //文本溢出部分用省略号表示
          display: -webkit-box; //特别显示模式
          -webkit-line-clamp: 1; //行数
          -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
    }
    .right-bnt{
      // float: right;
      position: absolute;
      right: 0;
      top: -5px;
    }
  }
  .centre {
    display: flex;
    .left {
      // position: relative;
      display: flex;
      align-items:center;
      justify-content: space-between;
      width: 230px;
      margin-right: 14px;
      h1 {
        font-size: 16px;
        margin: 0;
      }
    }
    .right {
      border-radius: 4px;
      overflow: hidden;
    }
  }

}
</style>
